package com.livemeter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;



/*
 * ProgressBar which shows text on top.
 * Code (slightly modified) by: http://colintmiller.com/2010/10/how-to-add-text-over-a-progress-bar-on-android/
 * */
public class ExtendedProgressBar extends ProgressBar {  
    private String text;  
    private Paint textPaint;  
  
    public ExtendedProgressBar(Context context) {  
        super(context);  
        text = "HP";  
        textPaint = new Paint();  
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.BLACK);  
    }  
  
    public ExtendedProgressBar(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        text = "HP";  
        textPaint = new Paint();  
        textPaint.setColor(Color.BLACK);  
    }  
  
    public ExtendedProgressBar(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        text = "HP";  
        textPaint = new Paint();  
        textPaint.setColor(Color.BLACK);  
    }  
  
    @Override  
    protected synchronized void onDraw(Canvas canvas) {  
        
    	
    	// First draw the regular progress bar, then custom draw our text  
        super.onDraw(canvas);  
        Rect bounds = new Rect();  
        textPaint.getTextBounds(text, 0, text.length(), bounds);  
        int x = getWidth() / 2 - bounds.centerX();  
        int y = getHeight() / 2 - bounds.centerY();  
        canvas.drawText(text, x, y, textPaint);  
    }  

    @Override
    public void setMax(int max)
    {
    	super.setMax(max);
    	
    	this.changeText();
    }

    @Override
    public void setProgress(int progressValue)
    {
    	// Nur monoton steigende Progress Werte?
    	super.setProgress(0);
    	super.setProgress(progressValue);
    	this.changeText();
    }
    
    /*
     * Sets percentage of votes (progressValue / max) as text.
     * */
    private void changeText()
    {
    	this.text = Math.round(( ((float)((float)this.getProgress() / this.getMax())) * 100)) + " %";
    }
    
	private synchronized void setText(String text) {  
        this.text = text;  
        drawableStateChanged();  
    }  
  
    public void setTextColor(int color) {  
        textPaint.setColor(color);  
        drawableStateChanged();  
    }  
}  